<template>
  <a-modal
    title="价格修改申请"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    cancelText="关闭"
    style="top:20px;"
  >
    <a-spin :spinning="confirmLoading">

      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="24" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="订单编号">
              {{ this.model.id }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="应付金额">
              {{ this.model.payAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="运费">
              {{ this.model.freightAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="促销">
              {{ this.model.promotionAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="积分抵扣">
              {{ this.model.integrationAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="优惠券">
              {{ this.model.couponAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="红包">
              {{ this.model.redbagAmount }}
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="申请后金额">
              {{ applyTotalAmount }}
            </a-form-item>
          </a-col>
        </a-row>

      </a-form>



      <!--<a-table
        style="margin-bottom: 24px"
        rowKey="id"
        bordered
        :scroll="{ x: 800 }"
        :columns="goodsColumns"
        :pagination="false"
        :dataSource="goodsData">

      </a-table>-->

      <j-editable-table
        ref="editableTable1"
        :columns="goodsColumns"
        :dataSource="goodsData"
        @valueChange="handleValueChange"
        :maxHeight="300"
        :rowNumber="false"
        :rowSelection="false"
        :actionButton="false"/>
    </a-spin>
  </a-modal>
</template>

<script>
  import pick from 'lodash.pick'
  import { httpAction } from '@/api/manage'
  import { getAction } from '@/api/manage'
  import JEditableTable from '@/components/jeecg/JEditableTable'
  import { FormTypes, VALIDATE_NO_PASSED, getRefPromise, validateFormAndTables } from '@/utils/JEditableTableUtil'


  export default {
    name: "StmOrderEditApplyModal",
    components: {
      JEditableTable
    },
    data () {
      return {
        visible: false,
        confirmLoading: false,
        applyTotalAmount:Number,
        model: {},
        goodsColumns: [
          {
            title: '名称',
            key: 'productName',
            dataIndex: 'productName'
          },
          {
            title: '货号',
            key: 'productSn',
            dataIndex: 'productSn'
          },
          {
            title: '商品类型',
            key: 'orderProductType_dictText',
            dataIndex: 'orderProductType_dictText'
          },
          {
            title: '商品单价',
            key: 'productPrice',
            dataIndex: 'productPrice'
          },
          {
            title: '小计',
            key: 'totalRealAmount',
            dataIndex: 'totalRealAmount'
          },
          {
            title: '数量',
            key: 'productQuantity',
            dataIndex: 'productQuantity'
          },
          {
            title: '申请商品价格',
            type: FormTypes.input,
            key: 'applyAmount',
            dataIndex: 'applyAmount'
          }
        ],
        goodsData:[],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 13 },
        },
        form:this.$form.createForm(this),
        url: {
          edit: "/stm/omsOrder/editiItemPrice",
          omsOrderItem: {
            list: '/stm/omsOrder/queryOmsOrderItemByMainId'
          },
        },
      }
    },
    created () {
      console.log("created");
    },
    methods: {
      show(record) {
        //this.form.resetFields();
        this.visible = true;
        this.model =  Object.assign({}, record);
        this.applyTotalAmount=0;
        this.goodsData = [];
        if (this.model.id) {
          console.log(this.model.id);
          this.loadGoodsData();
        }
        /*this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,''))
        });*/
      },
      loadGoodsData() {
        var params = { id: this.model.id };
        this.loading = true;
        getAction(this.url.omsOrderItem.list, params).then((res) => {
          if (res.success) {
            this.goodsData = res.result;
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },
      handleValueChange(value){
        if(null !== value.row){
          console.log(value.row)
          this.handleTotalAmount(value.row)
        }
      },
      handleTotalAmount(row) {
        let applyPayAmount = 0
        this.goodsData.forEach(column => {
          if(column.id == row.id) {
            if(column.orderProductType !== '0'){
              this.$message.warning("赠品价格不可以修改");
              return;
            }
            console.log(column)
            column.applyAmount = row.applyAmount
            column.promotionAmount = column.productPrice - column.applyAmount
            column.totalPromotionAmount = column.promotionAmount * column.productQuantity
            console.log("column.applyAmount:" + column.applyAmount)
            let totalPromotionAmount = (column.totalPromotionAmount === null ? 0 : column.totalPromotionAmount)
            console.log("totalPromotionAmount:" + totalPromotionAmount)
            let totalCouponAmount = (column.totalCouponAmount === null ? 0 : column.totalCouponAmount)
            console.log("totalCouponAmount:" + totalCouponAmount)
            let totalIntegrationAmount = (column.totalIntegrationAmount === null ? 0 : column.totalIntegrationAmount)
            console.log("totalIntegrationAmount:" + totalIntegrationAmount)
            let totalRedbagAmount = (column.totalRedbagAmount === null ? 0 : column.totalRedbagAmount)
            console.log("totalRedbagAmount:" + totalRedbagAmount)

            column.totalRealAmount = (column.productPrice * column.productQuantity)- totalPromotionAmount - totalCouponAmount - totalIntegrationAmount - totalRedbagAmount


          }
          applyPayAmount = applyPayAmount + column.totalRealAmount


        })
        this.applyTotalAmount = applyPayAmount.toFixed(2)
      },

      close () {
        this.form.resetFields();
        this.$emit('close');
        this.visible = false;
      },
      handleSubmit () {
        this.confirmLoading = true;
        let itemMap = {};
        this.goodsData.forEach(row => {
          itemMap[row.id] = row.applyAmount
        })
        let formData = {
          orderId: this.model.id,
          map: itemMap
        }
        console.log(formData)
        httpAction(this.url.edit,formData,'put').then((res)=>{
          if(res.success){
            this.$message.success(res.message);
            this.$emit('ok');
          }else{
            this.$message.warning(res.message);
          }
        }).finally(() => {
          this.confirmLoading = false;
          this.close();
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>